package com.sgzn.sgpay.service.tcp.impl;

import com.sgzn.sgpay.service.tcp.SgTcpProtocolHead;
import com.sgzn.sgpay.service.tcp.SgTcpServer;
import com.sgzn.sgpay.service.tcp.api.TcpRefundService;
import com.sgzn.sgpay.service.tcp.model.TcpRefundRequest;
import com.sgzn.sgpay.service.tcp.model.TcpRefundResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.Socket;

/**
 * Created by xxjs-gd-llf
 * DATETIME:2018/4/29 17:07
 * Description:
 */
@Service
@Slf4j
public class TcpRefundServiceImpl implements TcpRefundService {

  @Override
  public   void tcpAnswerRefund(String  recvBuffer, Socket socket){
      TcpRefundRequest tcpRefundRequest = decodeRefundBuffer(recvBuffer);

      SgTcpServer.socketList.put(tcpRefundRequest.getDeviceId(),socket);

      if (tcpRefundRequest != null){
          log.info("维持Socket长连接成功");
      }
      //调用退款接口 待开发

      //回应退款
      TcpRefundResponse tcpRefundResponse = handleRefundRequest(tcpRefundRequest);
      try {
          log.info("发送退款响应报文:{}",tcpRefundResponse );
          socket.getOutputStream().write(tcpRefundResponse.toString().getBytes("UTF-8"));
      }catch (IOException e ){
          e.printStackTrace();
      }
    }



    /**
     * 解析收到的数据
     * @param recvBuffer
     * @return
     */
    private TcpRefundRequest decodeRefundBuffer(String recvBuffer){
        TcpRefundRequest tcpRefundRequest = new TcpRefundRequest();

        //命令代码
        tcpRefundRequest.setCommandCode(recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[1] );

        //设备ID
        tcpRefundRequest.setDeviceId( recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[2] );

        //退款流水
        tcpRefundRequest.setRefundId(recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[3]);

        //申请退款金额
        tcpRefundRequest.setAmount(recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[4]);


        //检验和
        tcpRefundRequest.setCheckSumCode(recvBuffer.split(SgTcpProtocolHead.TCP_PROTOCOL_SEPERATOR)[5]);

        //校验和检查
        if(!tcpRefundRequest.getCheckSumCode().equals(SgTcpProtocolHead.DEFAULT_PROTOCOL_CHECKSUM )){
            log.info("校验和错误. {},{}",tcpRefundRequest.getCheckSumCode(),SgTcpProtocolHead.DEFAULT_PROTOCOL_CHECKSUM);
            return  null;
        }
        return  tcpRefundRequest;
    }

    /**
     * 根据请求拼装响应报文
     * @param request
     * @return
     */
    private TcpRefundResponse handleRefundRequest(TcpRefundRequest request ){
        //应答
        TcpRefundResponse tcpRefundResponse = new TcpRefundResponse();
        tcpRefundResponse.setBeginCode(SgTcpProtocolHead.TCP_PROTOCOL_HEAD);
        tcpRefundResponse.setCommandCode(SgTcpProtocolHead.TCP_REFUND_RESPONSE);
        tcpRefundResponse.setDeviceId( request.getDeviceId() );
        tcpRefundResponse.setRefundId(request.getRefundId());
        tcpRefundResponse.setActualAmount(request.getAmount()); //
        tcpRefundResponse.setCheckSumCode(SgTcpProtocolHead.DEFAULT_PROTOCOL_CHECKSUM);
        tcpRefundResponse.setEndCode(SgTcpProtocolHead.TCP_PROTOCOL_END);
        return  tcpRefundResponse;
    }

}
